package string

import "strconv"

// https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnpvdm/
// 描述字符串
// 算法: 递归
func CountAndSay(n int) string {
	if 1 == n {
		return "1"
	}
	currentStr := "1"
	for index := 2; index <= n; index++ {
		charCount := 1
		countIndex := 0
		nextStr := ""
		for countIndex < len(currentStr) {
			if countIndex+1 >= len(currentStr) || currentStr[countIndex] != currentStr[countIndex+1] {
				nextStr += strconv.Itoa(charCount) + currentStr[countIndex:countIndex+1]
				charCount = 1
			} else {
				charCount++
			}
			countIndex++
		}
		currentStr = nextStr
	}
	return currentStr
}
